<template>
  <div/>
</template>

<script>
import {
  addEntity,
  deleteEntities,
  deleteEntity,
  editEntity,
  exportFile,
  getEntity,
  getList, post, get
} from '@/api/base-api'
import { btnPermission } from '@/utils/btn-permission'
import fileDownload from 'js-file-download'
import { Guid } from '@/constants/guid'

export default {
  name: 'BasePage',
  data() {
    return {
      url: {
        queryList: null,
        queryEntity: null,
        addEntity: null,
        editEntity: null,
        deleteEntity: null,
        deleteEntities: null
      },
      addDisabled: false,
      editDisabled: true,
      delDisabled: true,
      multipleSelection: null,
      dialogFormVisible: false,
      title: null,
      loading: false,
      tableData: [],
      queryCondition: {
        PageIndex: 0,
        PageSize: 10,
        Sort: 'CreateTime DESC',
        TotalCount: 0,
        QueryItems: []
      }
    }
  },
  watch: {},
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    this.initData()
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    indexMethod(index) {
      return this.queryCondition.PageIndex * this.queryCondition.PageSize + index + 1
    },
    handleSizeChange(val) {
      this.queryCondition.PageSize = val
      this.initData()
    },
    handleCurrentChange(val) {
      this.queryCondition.PageIndex = val - 1
      this.initData()
    },
    handleSelectionChange: function(val) {
      if (val.length === 0) {
        this.editDisabled = true
        this.delDisabled = true
        this.multipleSelection = null
        return
      }
      this.multipleSelection = val
      if (val.length === 1) {
        this.editDisabled = false
      } else {
        this.editDisabled = true
      }
      this.delDisabled = false
    },
    initData: function() {
      this.loading = true
      this.getList(this.queryCondition).then(response => {
        this.tableData = response.list
        this.queryCondition.TotalCount = response.totalCount
        this.loading = false
      })
    },
    getList: function(param) {
      return getList(this.url.queryList, param)
    },
    getEntity: function(param) {
      return getEntity(this.url.queryEntity, param)
    },
    addEntity: function(param) {
      return addEntity(this.url.addEntity, param)
    },
    editEntity: function(param) {
      return editEntity(this.url.editEntity, param)
    },
    deleteEntity: function(param) {
      return deleteEntity(this.url.deleteEntity, param)
    },
    deleteEntities: function(param) {
      return deleteEntities(this.url.deleteEntities, param)
    },
    resetQueryFrom: function() {
      this.$refs.queryForm.resetFields()
      this.queryCondition.QueryItems = []
      this.initData()
    },
    btnPermission: function(code) {
      return btnPermission(code)
    },
    del: function() {
      this.$confirm('确定要将数据删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.multipleSelection.length === 1) {
          this.deleteEntity(this.multipleSelection[0].id).then(response => {
            this.$message({ showClose: true, message: '删除成功', type: 'error' })
            this.editDisabled = true
            this.delDisabled = true
            this.initData()
          })
        } else {
          const ids = this.multipleSelection.map((item) => {
            return item.id
          })
          this.deleteEntities(ids.toString()).then(response => {
            this.$message({ showClose: true, message: '删除成功', type: 'error' })
            this.editDisabled = true
            this.delDisabled = true
            this.initData()
          })
        }
      })
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id === Guid.Empty) {
            this.addEntity(this.form).then(response => {
              this.$message({ showClose: true, message: '保存成功', type: 'success' })
              this.dialogFormVisible = false
              this.editDisabled = true
              this.delDisabled = true
              this.initData()
            })
          } else {
            this.editEntity(this.form).then(response => {
              this.$message({ showClose: true, message: '编辑成功', type: 'success' })
              this.dialogFormVisible = false
              this.editDisabled = true
              this.delDisabled = true
              this.initData()
            })
          }
        }
      })
    },
    exportFile: function(url, param, filename) {
      exportFile(url, param).then(res => {
        fileDownload(res.data, filename)
      })
    },
    post: function(url, param) {
      return post(url, param)
    },
    get: function(url, param) {
      return get(url, param)
    }
  }
}
</script>

<style scoped>

</style>
